/**
 * 二维码识别
 */
!function (e, $, mui) {
	function quickMark(config) {
		var t = this;
		var defaultConfig = {
			id: 'scan-screen',
			filter: [plus.barcode.QR],
			callback: function ( type, code, file ) {
				// loaded code 
				alert(code);
			}
		};
		t.config = $.extend({}, defaultConfig, config);
		console.log(JSON.stringify(t.config));
		t.element = {};
		
		// 初始化
		t.init();
	}
	
	var pt = quickMark.prototype;
	
	// 初始化
	pt.init = function () {
		var t = this;
		t.element.container = $('#quick-mark-container');
		if(t.element.container.length == 0) {
			var html = '';
			html += '<style>';
			html += '.quick-mark-container {display: none; background: #000; height: 100%; width: 100%; position: fixed; top: 0; left: 0;}';
			html += '#scan-screen {width: 100%; height: 500px; margin-top: 0;}';
			html += '.quick-mark-container .action {position: absolute; bottom: 0; width: 100%; background: rgba(0,0,0,0.99); color: #FFFFFF; height: 60px;}';
			html += '.quick-mark-container .action .active { color: blue;}';
			html += '</style>';
			html += '<div class="quick-mark-container">';
			html += '<header class="page-header mui-bar mui-bar-nav hp-bar">';
			html += '<a class="mui-action-back mui-pull-left mui-icon mui-icon-arrowleft"></a>';
			html += '<h1 id="bulidTitle" class="mui-title">二维码识别</h1></header><div id="scan-screen"></div>';
			html += '<div class="action">';
			html += '<div class="camera mui-col-xs-6 active mui-pull-left mui-text-center p-10" href="javascript:void(0);">';
			html += '<em class="mui-icon-extra mui-icon-extra-sweep"></em><br>';
			html += '扫码';
			html += '</div>';
			html += '<div class="local mui-col-xs-6 mui-pull-left mui-text-center p-10" href="javascript:void(0);">';
			html += '<em class="mui-icon mui-icon-image"></em><br>';
			html += '相册';
			html += '</div>';
			html += '</div>';
			html += '</div>';
			t.element.container = $(html).appendTo('body');
		};
		t.element.local = t.element.container.find('.local');
		t.element.local.on('tap', function () {
			t.gallery();
		});
	}
	
	// 开始扫描
	pt.scanStart = function () {
		var t = this;
		t.element.container.show();
		if(!t.bc) {
			t.bc = new plus.barcode.Barcode( t.config.id, t.config.filter);
			t.bc.onmarked = function ( type, code, file ) {
				// loaded code 
				t.config.callback( type, code, file );
				t.close();
			}
		}
		t.bc.start();
	}
	
	// 从相册中选择二维码图片 
	pt.gallery = function () {
		var t = this;
	    plus.gallery.pick(function(path){
	    	console.log(path);
		    plus.barcode.scan(path, function ( type, code, file ) {
				// loaded code 
				console.log(code);
				t.config.callback( type, code, file );
				t.close();
			}, function(error){
				console.log(JSON.stringify(error));
				plus.nativeUI.alert( "无法识别此图片" );
				t.close();
			});
	    }, t.config.filter);
	}
	
	// 关闭扫描
	pt.close = function () {
		var t = this;
		if(t.bc) {
			t.bc.close();
			delete t.bc;
		}
		t.element.container.hide();
	}
	
	// 初始化
	mui.plusReady(function () {
		e.quickMark = quickMark;
	});
}(window, $, mui);
